package com.yizhiliulianta.movie.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.yizhiliulianta.movie.application.convert.MovieDTOConverter;
import com.yizhiliulianta.movie.application.dto.MovieDTO;
import com.yizhiliulianta.movie.common.entity.Result;
import com.yizhiliulianta.movie.domain.bo.MovieBO;
import com.yizhiliulianta.movie.domain.service.MovieDomainServcie;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/movie")
@Slf4j
public class MovieController {

    @Resource
    private MovieDomainServcie movieDomainServcie;

    @PostMapping("/getMovie")
    public Result<MovieDTO> getMovie(@RequestBody MovieDTO movieDTO, HttpServletRequest httpServletRequest) {
        try {
            if (log.isInfoEnabled()) {
                log.info("MovieController.getMovie.dto:{}", JSON.toJSONString(movieDTO));
            }
            System.out.println(httpServletRequest.getHeader("loginId"));
            Preconditions.checkNotNull(movieDTO.getId(), "电影id不能为空");
            // 吞入前的数据映射
            MovieBO movieBO = MovieDTOConverter.INSTANCE.convertDtoToMovieBO(movieDTO);
            // 吞入
            MovieBO bo = movieDomainServcie.selectMovie(movieBO);
            //吐出前的数据映射
            MovieDTO movie = MovieDTOConverter.INSTANCE.convertBoToMovieDTO(bo);
            //吐出
            return Result.ok(movie);
        } catch (Exception e) {
            log.error("MovieController.getMovie.error{}", e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }


}
